﻿using mshtml;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Arb
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {

            String str = @"server=localhost;database=arb;userid=root;password=;";
            MySqlConnection con = null;
            con = new MySqlConnection(str);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("DELETE FROM html", con);
            cmd.ExecuteNonQuery();

            /*
            String cmdText = "SELECT bookie_id, bookie_name FROM bookie";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader.GetString(0));
            }
            */

            pinnacle(con);
            betonline(con); 
            youwin(con);
            betathome(con);
            betway(con);
            SkyBet(con);
            Sportingbet(con);
            Betfair(con);
            Bet365(con);
            Bwin(con);
            Betclic(con);
            Williamhill(con);
            NetBet(con);
            expekt(con);
            
            /*
            Thread threadBet365 = new Thread(Bet365);
            threadBet365.SetApartmentState(ApartmentState.STA);
            threadBet365.IsBackground = false;
            threadBet365.Start();
            threadBet365.Join();
            
            Thread threadBwin = new Thread(Bwin);
            threadBwin.SetApartmentState(ApartmentState.STA);
            threadBwin.IsBackground = false;
            threadBwin.Start();
            threadBwin.Join();

            Thread threadBetclick = new Thread(Betclick);
            threadBetclick.SetApartmentState(ApartmentState.STA);
            threadBetclick.IsBackground = false;
            threadBetclick.Start();
             */ 
            //Console.ReadKey();

            /*
            string url = "http://localhost/index.php/tennis";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url);
            */
            
        }

        static void pinnacle(MySqlConnection con)
        {
            Console.WriteLine("pinnacle thread starting");

            string url = "https://www.pinnaclesports.com/";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;

            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(10));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            List<string> hreflist = new List<string>();

            HtmlElementCollection divelements = wb.Document.GetElementsByTagName("div");
            foreach (HtmlElement divelement in divelements)
            {
                if (divelement.GetAttribute("className") == "clr" && divelement.InnerHtml != null && divelement.InnerHtml.Trim().Contains("Tennis"))
                {
                    HtmlElementCollection aelements = divelement.NextSibling.GetElementsByTagName("a");
                    foreach (HtmlElement aelement in aelements)
                    {
                        //Console.WriteLine(aelement.GetAttribute("href"));
                        hreflist.Add(aelement.GetAttribute("href"));
                    }
                }
            }

            dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            int ii = 1;
            foreach(string href in hreflist)
            {
                Console.WriteLine("pinnacle tournaments clicking - " + href);
                WebBrowser wbsub = new WebBrowser();
                wbsub.ScrollBarsEnabled = false;
                wbsub.ScriptErrorsSuppressed = true;
                wbsub.Navigate(href, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");
                DateTime dueTimesub = DateTime.Now.Add(TimeSpan.FromSeconds(5));
                while (DateTime.Now < dueTimesub)
                {
                    Application.DoEvents();
                }
                HTMLDocument objHtmlDoc = (HTMLDocument)wbsub.Document.DomDocument;
                string pinnaclestring = objHtmlDoc.documentElement.innerHTML;

                //System.IO.File.WriteAllText(@"C:\Temp\pinnacle" + ii.ToString() + ".html", pinnaclestring);
                String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
                MySqlCommand cmd = new MySqlCommand(cmdText, con);
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@html_bookie_id", "14");
                cmd.Parameters.AddWithValue("@html_sport_id", "2");
                cmd.Parameters.AddWithValue("@html_text", pinnaclestring);
                cmd.ExecuteNonQuery(); //execute the mysql command
                ii++;
            }
            Console.WriteLine("pinnacle thread exiting");

        }

        static void betonline(MySqlConnection con)
        {
            Console.WriteLine("betathome thread starting");

            string url = "http://www.betonline.com/sportsbook";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;

            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(10));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HtmlElementCollection classButton = wb.Document.GetElementsByTagName("a");
            foreach (HtmlElement aelement in classButton)
            {
                //if (aelement.GetAttribute("title") != "") Console.WriteLine(aelement.GetAttribute("title"));
                if (aelement.GetAttribute("className") == "subNavA" && aelement.GetAttribute("cfg").Contains("Tennis"))
                {
                    if(aelement.Parent.NextSibling.GetAttribute("className") == "ckbxWrapper") 
                    {
                        aelement.Parent.NextSibling.FirstChild.InvokeMember("click");
                    } else 
                    {
                        aelement.Parent.NextSibling.NextSibling.FirstChild.InvokeMember("click");
                    }
                    Console.WriteLine("betonline click");
                }
            }

            wb.Document.GetElementById("viewSelectedId").InvokeMember("click");
            Console.WriteLine("betonline formsubmit click");

            dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }



            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            System.IO.File.WriteAllText(@"C:\Temp\betonline.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "13");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("betonline thread exiting");
        }

        static void youwin(MySqlConnection con)
        {
            Console.WriteLine("youwin thread starting");

            string url = "http://www.youwin.com/en/betting/tennis-upcoming-matches-doubles-matches";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url, null, null, "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); 
            //while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(10));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            System.IO.File.WriteAllText(@"C:\Temp\youwin.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "12");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("youwin thread exiting");
        }

        static void betathome(MySqlConnection con)
        {
            Console.WriteLine("betathome thread starting");

            string url = "https://www.bet-at-home.com/en/sport";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;

            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            // a.class i-sprite seb-i-control i-checkBlue15x14  -- title - Select all Tennis events

            HtmlElementCollection classButton = wb.Document.GetElementsByTagName("a");
            foreach (HtmlElement aelement in classButton)
            {
                //if (aelement.GetAttribute("title") != "") Console.WriteLine(aelement.GetAttribute("title"));
                if (aelement.GetAttribute("title") == "Select all Tennis events" || aelement.GetAttribute("title") == "Összes Tenisz esemény kiválasztása")
                {
                    aelement.InvokeMember("click");
                    Console.WriteLine("betathome click");
                }
            }

            dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HtmlElementCollection afbo_text = wb.Document.GetElementsByTagName("a");
            foreach (HtmlElement aelement in afbo_text)
            {
                //if (aelement.GetAttribute("title") != "") Console.WriteLine(aelement.GetAttribute("title"));
                if (aelement.GetAttribute("className") == "fbo-text" && aelement.GetAttribute("name") == "200")
                {
                    aelement.InvokeMember("click");
                    Console.WriteLine("betathome click on typea");
                }
            }

            dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\betathome.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "11");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("betathome thread exiting");
        }

        static void betway(MySqlConnection con)
        {
            Console.WriteLine("betway thread starting");

            string url = "http://betway.com/sports-betting#/coupon/tennis-matches-all";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(10));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\betway.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "10");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("betway thread exiting");
        }

        static void SkyBet(MySqlConnection con)
        {
            Console.WriteLine("SkyBet thread starting");

            string url = "http://www.skybet.com/tennis/coupon/match-betting";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(10));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\SkyBet.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "9");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("SkyBet thread exiting");
        }

        static void Williamhill(MySqlConnection con)
        {
            Console.WriteLine("Williamhill thread starting");

            string url = "http://sports.williamhill.com/bet/hu/betting/y/17/cp/380/Tenisz.html";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(10));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\Sportingbet.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "6");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("Williamhill thread exiting");
        }

        static void Sportingbet(MySqlConnection con)
        {
            Console.WriteLine("Sportingbet thread starting");

            string url = "http://hu.sportingbet.com/sports-todays-tennis/8-8.html";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(20));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\Sportingbet.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "3");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("Sportingbet thread exiting");
        }

        static void NetBet(MySqlConnection con) {
            Console.WriteLine("NetBet thread starting");

            string url = "https://sport.netbet.com/tennis/";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;

            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            /*
            while (wb.Document == null || wb.Document.GetElementsByTagName("head").Count == 0)
            {
                Application.DoEvents();
            }

            HtmlElement head = wb.Document.GetElementsByTagName("head")[0];
            HtmlElement scriptEl = wb.Document.CreateElement("script");
            IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
            string alertBlocker = @"window.alert = function () { }; 
                        window.print=function () { };
                        window.confirm=function () { };
                    ";
            element.text = alertBlocker;
            head.AppendChild(scriptEl);
             * */

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HtmlElementCollection classButton = wb.Document.GetElementsByTagName("li");
            foreach (HtmlElement lielement in classButton)
            {
                if (lielement.GetAttribute("className") == "top_leagues")
                {
                    HtmlElementCollection images = lielement.GetElementsByTagName("img");
                    foreach (HtmlElement imgelement in images) {
                        imgelement.InvokeMember("click");
                        Console.WriteLine("NetBet click");
                    }
                }
            }

            HtmlElementCollection aelements = wb.Document.GetElementsByTagName("a");
            foreach (HtmlElement aelement in aelements)
            {
                if (aelement.GetAttribute("className") == "siteButtons" && aelement.InnerHtml.Contains("View Selected Leagues"))
                {
                    aelement.InvokeMember("click");
                    Console.WriteLine("NetBet form submit click");
                }
            }

            dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            System.IO.File.WriteAllText(@"C:\Temp\NetBet.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "7");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("NetBet thread exiting");
        }

        static void Betfair(MySqlConnection con)
        {
            Console.WriteLine("Betfair thread starting");

            string url = "http://www.betfair.com/exchange/tennis";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HtmlElementCollection classButton = wb.Document.GetElementsByTagName("h3");
            foreach (HtmlElement element in classButton)
            {
                element.InvokeMember("click");
                Console.WriteLine("Betfair click");
            }

            dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\Betfair.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "4");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("Betfair thread exiting");
        }

        static void Bet365(MySqlConnection con)
        {
            Console.WriteLine("Bet365 thread starting");

            //string url = "https://mobile.bet365.com/premium/#type=CouponRow;key=13-1-50-2-163-0-0-0-1-0-0-4505-0-0-1-0-0-0-0;ip=1;lng=25";
            string url = "https://mobile.bet365.com/sport/coupon/?ptid=4505&key=13-1-50-2-163-0-0-0-1-0-0-4505-0-0-1-0-0-0-0&clvl=2";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            //wb.Navigate(url, null, null, "User-Agent: Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1\r\n");
            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            /*
            HtmlElementCollection classButton = wb.Document.All;
            foreach (HtmlElement element in classButton)
            {
                if (element.GetAttribute("className") == "sporticon_13")
                {
                    element.InvokeMember("click");
                    Console.WriteLine("click");
                }
            }
            */

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\Bet365.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "2");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("Bet365 thread exiting");
        }

        static void Bwin(MySqlConnection con) 
        {
            Console.WriteLine("Bwin thread starting");
            
            string url = "https://sports.bwin.com/en/sports#sportId=5";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");
            //while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\Bwin.html", source);

            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "1");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command
            
            //Console.WriteLine(source);

            Console.WriteLine("Bwin thread exiting");
        }

        static void Betclic(MySqlConnection con)
        {
            Console.WriteLine("Betclic thread starting");

            string url = "https://hu.betclic.com/naptar/tenisz-s2i1";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url, null, null, "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); 
            //while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(10));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\Betclic.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "5");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("Betclic thread exiting");
        }

        static void expekt(MySqlConnection con)
        {
            Console.WriteLine("expekt thread starting");

            string url = "https://en.expekt.com/sport/";
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;

            wb.Navigate(url, null, null, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n");

            DateTime dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HtmlElementCollection classButton = wb.Document.GetElementsByTagName("input");
            foreach (HtmlElement lielement in classButton)
            {
                if (lielement.GetAttribute("id") == "left-menu-sport-2")
                {
                    lielement.InvokeMember("click");
                    Console.WriteLine("expekt click");
                }
            }

            dueTime = DateTime.Now.Add(TimeSpan.FromSeconds(5));
            while (DateTime.Now < dueTime)
            {
                Application.DoEvents();
            }

            HTMLDocument objHtmlDoc = (HTMLDocument)wb.Document.DomDocument;
            string source = objHtmlDoc.documentElement.innerHTML;
            //System.IO.File.WriteAllText(@"C:\Temp\expekt.html", source);
            String cmdText = "INSERT INTO html (html_bookie_id, html_sport_id, html_text) VALUES(@html_bookie_id, @html_sport_id, @html_text)";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@html_bookie_id", "8");
            cmd.Parameters.AddWithValue("@html_sport_id", "2");
            cmd.Parameters.AddWithValue("@html_text", source);
            cmd.ExecuteNonQuery(); //execute the mysql command

            Console.WriteLine("expekt thread exiting");
        }

    }
}
